(defmeth histofreq-proto :choose-density ()

  (send self :add-slot 'kernel-width)

  (defmeth self :kernel-width (&optional (val nil set))
    (if set (setf (slot-value 'kernel-width) val))
    (slot-value 'kernel-width))

  (defmeth self :initial-kernel ()
    "Computes Silverman's rule of thumb"
    (let ((iq (/ (interquartile-range (send self :data)) 1.34))
          (st (standard-deviation (send self :data)))
          (min))
      (cond 
         ((and (> iq 0) (> st 0))
         (setf min (min iq st)))
        ((= iq 0)
        (setf min st))
        (t (setf min iq)))

    (* 1.06 (^ (length (send self :data)) (- (/ 1 5))) min)
      ))

  (send self :kernel-width (send self :initial-kernel))
  (send self :kernel-type 1)

  (when (> (send self :num-points) 2)
        (cond 
          ((not (send self :slot-value 'dens-dialog))
           (let* ((den-fun-state (list nil nil nil))
                  (monopoly-arguments nil)
                  (title  (send text-item-proto :new "Choose Curve"))
                  (Kernel-text (send text-item-proto :new "Kernel (Use Slider and Type): "))
                  (normal (send toggle-item-proto 
                    :new "Normal Density"
                    :value (select den-fun-state 0)
                    :action #'(lambda () 
                                (send self :switch-add-normal))))
                  (kernel-type (send choice-item-proto
                    :new(list "Bisquare" "Gaussian" "Triangular" "Uniform")
                    :value 1
                    :action #'(lambda () 
                                (send self :put-kernel-type))))
                  (kernel (send toggle-item-proto 
                    :new "Kernel Density"
                    :value (select den-fun-state 1)
                    :action #'(lambda ()
                      (send self :kernel-type (send kernel-type :value))
                      (send self :switch-add-kernel))))   
                  (kernel-width (send text-item-proto :new (format () "~,3f" (send self :kernel-width))))
                  (kernel-width-values  
                   (mapcar #'(lambda (x) 
                               (read-from-string (format () "~,3f" x)))
                           (sort-data (combine (send self :initial-kernel)
                                    (apply 'rseq  (list (/ (send self :initial-kernel) 9)
                                                        (* (send self :initial-kernel) 4)
                                                  100))))))
                  (kernel-width-scrollbar (send sequence-scroll-item-proto :new
                                                kernel-width-values
                                                :value 5
                                                :action #'(lambda (value)
                                                            (send self :put-kernel-width-value)
                                                            (when (send self :show-kernel)
                                                                  (send self :start-buffering)
                                                                  (send self :switch-add-kernel)
                                                                  (send self :switch-add-kernel)
                                                                  (send self :buffer-to-screen)))
                                                :text-item kernel-width))       
                  (monopoly (send toggle-item-proto 
                    :new "MonoPoly Density"
                    :value (select den-fun-state 2)))
                  (dialog (send dialog-proto 
                    :new (list title normal 
                               (list (list kernel 
                                           (list kernel-text
                                                 )
                                           (list kernel-type (list kernel-width kernel-width-scrollbar))
                                          )))
                    :title "Distribution Curve"))
                 )
             

             (defmeth self :put-kernel-width-value ()
               (send self :kernel-width (select kernel-width-values (send kernel-width-scrollbar :value)))
               (when (send self :show-kernel)
                     (send self :switch-add-kernel)
                     (send self :switch-add-kernel)))
             (send self :add-subordinate dialog)
             (send self :slot-value 'dens-dialog dialog)


             (defmeth self :put-kernel-type ()
               (send self :kernel-type (send kernel-type :value))
               (when (send self :show-kernel)
                     (send self :switch-add-kernel)
                     (send self :switch-add-kernel)))
             (send self :add-subordinate dialog)
             (send self :slot-value 'dens-dialog dialog))

           )
          (t
           (send (send self :slot-value 'dens-dialog) :show-window)))))



(defmeth histofreq-proto :add-kernel 
                         (type-number &key (color 'green) line-width (draw t))
  (let* ((type (case type-number
                 (0 'b)
                 (1 'g)
                 (2 't)
                 (3 'u)))
         (width (send self :kernel-width))
         (current-variable (first (send self :content-variables)))
         (npts (send self :num-points))
         (ndim (- (send self :num-variables) 1))
        ;(varx (send self :point-coordinate current-variable (iseq npts)))
         (varx (send self :data))
         (x (rseq (min varx) (max varx) 100))
         (maxyaxis (max (send self :range 1)))
         (y (second (kernel-dens varx :xvals 100 :type type :width width)))
        ;(z (plot-lines (iseq (length y)) y))
         (maxy (max y))
         (x-at-maxy (select x (first (last (order y)))))
         (freq (send self :point-coordinate  1
                           (iseq (send self :num-points))))
         (y (* y (/ (max freq) (max y)))) ;densities are converted to the scale of the histograms
         ;(y (* y (length (send self :data))))
         
         (overlay (first (send self :slot-value 'overlays)))
         )
    
    (when (not line-width) (setf line-width *line-width*))
  #|  (send self :add-points (append (repeat (list x) ndim) (list y)) 
          :draw nil
          :color 'red
          :width line-width)|#
(send self :add-lines (append (repeat (list x) ndim) (list y)) 
          :draw nil
          :color color ;(send self :kernel-curve-color)
          :width line-width)
    (when draw (send self :redraw))))